Algorithm String Hash

Meow King March 15, 2024 Updated: March 15, 2024 #algorithm #hash #string hash

Related Question: 841. 字符串哈希

My initial code (wrong):

#include <iostream>
using namespace std;

typedef unsigned long long ULL;

const int N = 1e5 + 10;

char str[N];
ULL h[N], p[N], P = 131;

ULL mhash(int l, int r) {
	return h[r] - h[l - 1];
}

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int n, m;
	cin >> n >> m;
	
	cin >> (str + 1);

	p[0] = 1;
	for (int i = 1; i <= n; i++) {
		h[i] = h[i - 1] + str[i] * P;
		p[i] = p[i - 1] * P;
	}

	while (m --) {
		int l1, r1, l2, r2;
		cin >> l1 >> r1 >> l2 >> r2;
		if (mhash(l1, r1) == mhash(l2, r2)) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	
	return 0;
}

The correct version:

ULL mhash(int l, int r) {
	return h[r] - h[l - 1] * p[r - l + 1];
}

and

h[i] = h[i - 1] * P + str[i];

According to ChatGPT:
my original version:

 While this does give a difference that is influenced by the characters within the substring, it does not correctly account for the position of the substring within the larger string. This means that substrings with the same characters but starting at different positions in the string could potentially have different hash values because the power of P associated with each character's position isn't correctly normalized.